package com.example.controller;

import com.example.common.Result;
import com.example.entity.Salary;
import com.example.service.SalaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 薪资控制器
 */
@RestController
@RequestMapping("/salary")
public class SalaryController {

    @Resource
    private SalaryService salaryService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Salary salary) {
        salaryService.add(salary);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        salaryService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        salaryService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody Salary salary) {
        salaryService.updateById(salary);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Salary salary = salaryService.selectById(id);
        return Result.success(salary);
    }
    
    /**
     * 根据员工ID查询薪资记录
     */
    @GetMapping("/selectByEmployeeId/{employeeId}")
    public Result selectByEmployeeId(@PathVariable Integer employeeId) {
        List<Salary> list = salaryService.selectByEmployeeId(employeeId);
        return Result.success(list);
    }
    
    /**
     * 根据月份查询薪资记录
     */
    @GetMapping("/selectBySalaryMonth/{salaryMonth}")
    public Result selectBySalaryMonth(@PathVariable String salaryMonth) {
        List<Salary> list = salaryService.selectBySalaryMonth(salaryMonth);
        return Result.success(list);
    }

    /**
     * 查询全部
     */
    @GetMapping("/selectAll")
    public Result selectAll(Salary salary) {
        List<Salary> list = salaryService.selectAll(salary);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Salary salary,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Salary> list = salaryService.selectAll(salary);
        PageInfo<Salary> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }
    
    /**
     * 批量发放薪资
     */
    @PutMapping("/pay/batch")
    public Result paySalaryBatch(@RequestBody List<Integer> ids) {
        salaryService.paySalaryBatch(ids);
        return Result.success();
    }
    
    /**
     * 生成薪资记录
     */
    @PostMapping("/generate/{salaryMonth}")
    public Result generateSalary(@PathVariable String salaryMonth) {
        salaryService.generateSalary(salaryMonth);
        return Result.success();
    }

    /**
     * 薪资统计接口
     */
    @GetMapping("/statistics")
    public Result statistics(
            @RequestParam(required = false) Integer departmentId,
            @RequestParam(required = false) Integer employeeId,
            @RequestParam(required = false) String startMonth,
            @RequestParam(required = false) String endMonth) {
        // 调用service层方法进行统计
        Map<String, Object> statistics = salaryService.getStatistics(departmentId, employeeId, startMonth, endMonth);
        return Result.success(statistics);
    }

    /**
     * 批量导入薪资数据
     */
    @PostMapping("/import")
    public Result importSalary(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> result = salaryService.importSalary(file);
            return Result.success(result);
        } catch (IOException e) {
            return Result.error("400", "导入失败: " + e.getMessage());
        }
    }
    
    /**
     * 下载薪资导入模板
     */
    @GetMapping("/template")
    public void downloadTemplate(HttpServletResponse response) {
        try {
            salaryService.generateImportTemplate(response);
        } catch (IOException e) {
            // 处理异常
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            try {
                response.getWriter().write("{\"code\":\"500\",\"msg\":\"下载模板失败: " + e.getMessage() + "\"}");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}